/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.excesys.exsecs.om.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.om.dto.SheetDto;
import com.excesys.exsecs.om.entity.OmICheckSheet;
import com.excesys.exsecs.om.service.OmICheckSheetService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 巡检单
 *
 * @author weiyj
 * @date 2023-03-01 14:10:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/omichecksheet")
@Tag(name = "巡检单管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OmICheckSheetController {

	private final OmICheckSheetService omICheckSheetService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param omICheckSheet 巡检单
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_get')")
	public R getOmICheckSheetPage(Page page, OmICheckSheet omICheckSheet, String flag) {
		return R.ok(omICheckSheetService.getSheetWithRelPage(page, omICheckSheet, flag));
	}

	/**
	 * 通过id查询巡检单
	 * @param stId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{stId}")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_get')")
	public R getById(@PathVariable("stId") Integer stId) {
		return R.ok(omICheckSheetService.getById(stId));
	}

	/**
	 * 通过id查询巡检单
	 * @param stId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/getSheetById")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_get')")
	public R getSheetById(Integer stId) {
		return R.ok(omICheckSheetService.getSheetById(stId));
	}

	/**
	 * 通过id查询巡检单
	 * @param stId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/getSheetDetailById")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_get')")
	public R getSheetDetailById(Integer stId) {
		return R.ok(omICheckSheetService.getSheetDetailById(stId));
	}

	/**
	 * 通过id查询巡检单
	 * @param stId id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/getSheetDetailByNum")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_get')")
	public R getSheetDetailByNum(Integer stId) {
		return R.ok(omICheckSheetService.getSheetDetailByNum(stId));
	}

	/**
	 * 新增巡检单
	 * @param sheetDto 巡检单
	 * @return R
	 */
	@Operation(summary = "新增巡检单", description = "新增巡检单")
	@SysLog("新增巡检单")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_add')")
	public R save(@RequestBody SheetDto sheetDto) {
		return R.ok(omICheckSheetService.saveSheet(sheetDto));
	}

	/**
	 * 修改巡检单
	 * @param sheetDto 巡检单
	 * @return R
	 */
	@Operation(summary = "修改巡检单", description = "修改巡检单")
	@SysLog("修改巡检单")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_edit')")
	public R updateById(@RequestBody SheetDto sheetDto) {
		return R.ok(omICheckSheetService.updateSheet(sheetDto));
	}

	/**
	 * 通过id删除巡检单
	 * @param stId id
	 * @return R
	 */
	@Operation(summary = "通过id删除巡检单", description = "通过id删除巡检单")
	@SysLog("通过id删除巡检单")
	@DeleteMapping("/{stId}")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_del')")
	public R removeById(@PathVariable Integer stId) {
		return R.ok(omICheckSheetService.removeById(stId));
	}

	/**
	 * 操作巡检单
	 * @param sheetDto 巡检单
	 * @return R
	 */
	@Operation(summary = "操作巡检单", description = "操作巡检单")
	@SysLog("操作巡检单")
	@PostMapping("/operate")
	// @PreAuthorize("@pms.hasPermission('om_omworder_add')" )
	public R operateOmICheckSheet(@RequestBody SheetDto sheetDto) {

		return R.ok(omICheckSheetService.operateOmICheckSheet(sheetDto));
	}

	/**
	 * 删除多个投诉
	 * @param idList 删除多个投诉
	 * @return R
	 */
	@Operation(summary = "删除多个巡检单", description = "删除多个巡检单")
	@SysLog("删除多个巡检单")
	@DeleteMapping("/removeByIds")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_del')")
	public R removeByIds(@RequestBody List<Integer> idList) {
		return R.ok(omICheckSheetService.removeByIds(idList));
	}

	/**
	 * 修改巡检点 - nfc状态 0否 1是
	 */
	@Operation(summary = "修改巡检点nfc状态", description = "修改巡检点nfc状态")
	@SysLog("修改巡检点nfc状态")
	@GetMapping("/updateNfcStatus")
	@PreAuthorize("@pms.hasPermission('om_omicheckpoint_edit')")
	public R getUpdateNfcStatus(@RequestParam Map map) {
		int data = omICheckSheetService.getUpdateNfcStatus(map);
		if (data == 0) {
			return R.ok("1",  "当前扫描设备不是该点位设备");
		}
		return R.ok(data, "修改成功");
	}

	/**
	 * 查询今日已巡检/未巡检
	 */
	@Operation(summary = "查询今日已巡检/未巡检", description = "查询今日已巡检/未巡检")
	@SysLog("查询今日已巡检/未巡检")
	@GetMapping("/getSheetNum")
	@PreAuthorize("@pms.hasPermission('om_omichecksheet_get')")
	public R getSheetNum() {
		return R.ok(omICheckSheetService.getSheetNum());
	}

}
